package com.shop.shoporder.OrderTimingTask;



import com.alibaba.fastjson.JSONObject;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserRequest;
import com.shop.shoporder.entity.order.Finace;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.logtype;
import com.shop.shoporder.service.*;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class OrderCheck {
    @Autowired
    OrderService orderService;
//    @Autowired
//    OrderListService orderListService;
//    @Autowired
//    AddressService addressService;
    @Autowired
ConfigService configService;
//    @Autowired
//    LogisticsService logisticsService;
//    @Autowired
//    OrderAddressService orderAddressService;
    @Autowired
CartService cartService;
    @Autowired
    FinaceService finaceService;
@Autowired
    PayOrderService payOrderService;

    @Scheduled(fixedRate=300000)//5分钟一次
    public void fixedDelay() {
        String ip = UserRequest.getServerIp();
        if (ip.contains("172.18.109.46")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date();
            String date = sdf.format(date1);
            Date dateX = addDateMinut(date, -1);
            List<Order> list = orderService.selectOrderCheck(null);
            Map<String, String> map= new HashMap<>();
            if (list != null && list.size() > 0) {
                for (Order order : list) {
                    if (order != null) {
                        int userNoId = order.getUsernoId();
                        String orderNo= order.getOrderNo();
                        double inter = order.getInter();
                        double costInter = order.getCostInter();
                        double cash = order.getCash();
                        map.put("userNoId",userNoId+"");
                        map.put("orderNo",orderNo+"");
                        map.put("inter",inter+"");
                        map.put("costInter",costInter+"");
                        map.put("cash",cash+"");
                        Response response=new Response();
                        String orderStatus = order.getOrderStatus() == null ? "0" : order.getOrderStatus();
                        int checkCount = order.getCheckCount() == null ? 0 : order.getCheckCount();
                        if (checkCount < 3) {//只检查3次
                            if (orderStatus.equals("701") || orderStatus.equals("706")) {
                                List<Finace> finaceList = null;
                                String orderNoStr="";
                                try {
                                    orderNoStr= URLEncoder.encode("微信充值"+orderNo,"UTF-8");
                                } catch (UnsupportedEncodingException e) {
                                    e.printStackTrace();
                                }
                                JSONArray jsonArr=finaceService.getWeChatList(userNoId,orderNoStr);
                                finaceList = (List<Finace>) JSONArray.toCollection(jsonArr, logtype.class);
                                if(finaceList!=null&&finaceList.size()>0){
                                    try {
                                        response = payOrderService.payInter(map,null);
                                        if (!response.getCode() .equals("000000")) {
                                            System.out.println("orderNo:" + order.getOrderNo());
                                            int coun = checkCount + 1;
                                            order.setCheckCount(coun);
                                            order.setPayErrorInfo("订单检查："+response.getErrorDetail());
                                            orderService.updateByPrimaryKeySelective(order);
                                        }
                                    } catch (Exception e) {
                                        int coun = checkCount + 1;
                                        order.setCheckCount(coun);
                                        order.setPayErrorInfo("订单检查E："+response.getErrorDetail());
                                        orderService.updateByPrimaryKeySelective(order);
                                    }
                                }else {
                                    int coun = checkCount + 1;
                                    order.setCheckCount(coun);
                                    order.setPayErrorInfo("订单未支付E");
                                    orderService.updateByPrimaryKeySelective(order);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 给时间加上几个小时
     * @param day 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public Date addDateMinut(String day, int hour){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return null;
        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;

    }


}
